package com.wanwan.baidu;

import com.baidu.aip.ocr.AipOcr;
import com.baidu.aip.util.Base64Util;
import org.json.JSONObject;
import utils.FileUtil;
import utils.HttpUtil;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;

/**
 * 百度AI文字识别工具类
 */
public class GeneralBasic {
    /**
     * 重要提示代码中所需工具类
     * FileUtil,Base64Util,HttpUtil,GsonUtils请从
     * https://ai.baidu.com/file/658A35ABAB2D404FBF903F64D47C1F72
     * https://ai.baidu.com/file/C8D81F3301E24D2892968F09AE1AD6E2
     * https://ai.baidu.com/file/544D677F5D4E4F17B4122FBD60DB82B3
     * https://ai.baidu.com/file/470B3ACCA3FE43788B5A963BF0B625F3
     * 下载
     */
    private static final String APP_ID = "19165652";
    private static final String APP_KEY = "gFCaKhQobX7R3SMvXqHOd7X3";
    private static final String SECRET_KEY = "twj5XUv9hiuMldf9uC8qf0I00XoItBBl";

    /**
     * 调用 百度 通用文字识别接口
     * @return
     */
    public static String generalBasic() {
        // 请求url
        //String url = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic";
        String url = "https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic";
        try {
            // 本地文件路径
            String filePath = "C:\\Users\\admin\\Desktop\\qrcode\\9.jpg";
            byte[] imgData = FileUtil.readFileByBytes(filePath);
            String imgStr = Base64Util.encode(imgData);
            String imgParam = URLEncoder.encode(imgStr, "UTF-8");

            HashMap<String, String> options = new HashMap<String, String>(16);
            options.put("language_type", "CHN_ENG");
            options.put("detect_direction", "true");
            options.put("detect_language", "true");
            options.put("probability", "true");

            String param = "image="+imgParam+"&detect_direction=true&language_type=CHN_ENG&detect_language=true&probability=true";

            // 注意这里仅为了简化编码每一次请求都去获取access_token，线上环境access_token有过期时间， 客户端可自行缓存，过期后重新获取。
            String accessToken = AuthService.getAuth();

            String result = HttpUtil.post(url, accessToken, param);
            System.out.println(result);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 百度AI文字识别 SDK
     * @param client
     */
    public static void sample(AipOcr client) throws IOException {
        // 传入可选参数调用接口
        HashMap<String, String> options = new HashMap<String, String>(16);
        options.put("language_type", "CHN_ENG");
        options.put("detect_direction", "true");
        options.put("detect_language", "true");
        options.put("probability", "true");

        // 可选：设置网络连接参数
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);

        // 参数为本地图片路径
        String image = "C:\\Users\\admin\\Desktop\\qrcode\\5.jpg";
        JSONObject res = client.basicGeneral(image, options);
        System.out.println(res.toString(2));

        // 通用文字识别, 图片参数为远程url图片
        //JSONObject res = client.basicGeneralUrl(url, options);
        //System.out.println(res.toString(2));
        // 可选：设置log4j日志输出格式，若不设置，则使用默认配置
        // 也可以直接通过jvm启动参数设置此环境变量
        System.setProperty("aip.log4j.conf", "log4j.properties");

    }

    public static void main(String[] args) throws IOException {
        AipOcr aipOcr = new AipOcr(APP_ID,APP_KEY,SECRET_KEY);
        //GeneralBasic.generalBasic();
        //GeneralBasic.sample(aipOcr);
        GeneralBasic.generalBasic();
    }
}
